package com.itheima.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.AdvertiseDto;
import com.itheima.excption.MyException;
import com.itheima.pojo.SmsHomeAdvertise;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SmsHomeAdvertiseService {
    @DubboReference
    private SmsHomeAdvertiseApi smsHomeAdvertiseApi;

    /**
     * 创建广告
     *
     * @param advertiseDto
     */
    public void create(SmsHomeAdvertise advertiseDto) {

        Boolean flag = smsHomeAdvertiseApi.save(advertiseDto);
        if (!flag) {
            //名称重复
            throw new MyException("广告名称重复!");
        }

    }

    /**
     * 根据id 删除
     *
     * @param ids
     */
    public void delete(Integer[] ids) {
        //删除之前,先进行判断选中的id 是否 有启用状态的
        for (Integer id : ids) {
            SmsHomeAdvertise byID = smsHomeAdvertiseApi.findByID(id);
            if (byID.getStatus() == 1) {
                throw new MyException("删除失败,请先将广告下线,再进行删除");
            }
        }
        smsHomeAdvertiseApi.delete(ids);

    }


    /**
     * 根据 到期时间 名称 类型联合查询
     *
     * @param pageNum
     * @param pageSize
     * @param endTime
     * @param name
     * @param type
     * @return
     */
    public PageResultVo<SmsHomeAdvertise> list(Integer pageNum, Integer pageSize, String endTime, String name, Integer type) {
        PageResultVo<SmsHomeAdvertise> pageResultVo = new PageResultVo<>();
        IPage<SmsHomeAdvertise> pageSms = smsHomeAdvertiseApi.list(pageNum, pageSize, endTime, type, name);
        pageResultVo.setPageNum(pageNum);
        pageResultVo.setPageSize(pageSize);
        Long total = pageSms.getTotal();
        pageResultVo.setTotal(total.intValue());
        Long pages = pageSms.getPages();
        pageResultVo.setTotalPages(pages.intValue());
        //针对数据集合做出单独的封装
        List<SmsHomeAdvertise> records = pageSms.getRecords();
        List<SmsHomeAdvertise> smsHomeAdvertises = records.stream().map((item) -> {
            SmsHomeAdvertise smsHomeAdvertise = new SmsHomeAdvertise();
            BeanUtils.copyProperties(item, smsHomeAdvertise);
            return smsHomeAdvertise;
        }).collect(Collectors.toList());
        pageResultVo.setList(smsHomeAdvertises);
        return pageResultVo;
    }


    /**
     * 修改状态
     *
     * @param id
     * @param status
     */
    public void updateStatus(Integer id, Integer status) {
        smsHomeAdvertiseApi.update(id, status);

    }


    /**
     * 修改广告
     *
     * @param smsHomeAdvertise
     */
    public void update(SmsHomeAdvertise smsHomeAdvertise) {
        SmsHomeAdvertise smsHomeAdvertise1 = smsHomeAdvertiseApi.findByID(smsHomeAdvertise.getId().intValue());
        if (smsHomeAdvertise1.getName().equals(smsHomeAdvertise.getName())) {
            //广告名未修改 ,直接不用判断 直接修改
            //throw new MyException("新的广告名和旧的广告名一样哦");
            smsHomeAdvertiseApi.updateByIdNo(smsHomeAdvertise);
            return;
        }
        Boolean aBoolean = smsHomeAdvertiseApi.updateById(smsHomeAdvertise);
        if (!aBoolean) {
            throw new MyException("广告名已经有了哦,请换一个名字");
        }

    }


    /**
     * 根据id 获取广告详情
     *
     * @param id
     * @return
     */
    public SmsHomeAdvertise findById(Integer id) {
        SmsHomeAdvertise byID = smsHomeAdvertiseApi.findByID(id);
        return byID;
    }
}
